package com.yunmai11.yunmai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yunmai11.yunmai.R;
import com.yunmai11.yunmai.bean.OrderBean;

import java.util.List;

/**
 * Created by Administrator on 2017/1/19.
 */
public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.MyViewHolder>{

    private Context context;
    private List<OrderBean> list;
    private MyOnItemClickListener listener;

    public interface MyOnItemClickListener {
        void onClick(View v, int position);
    }

    public void addOnItemClickListener(MyOnItemClickListener listener) {
        this.listener = listener;
    }

    public OrderListAdapter(Context contex, List<OrderBean> list) {
        this.context = contex;
        this.list = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.order_item, parent, false);
        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        holder.tv_id.setText(list.get(position).orderId);
        holder.tv_name.setText(list.get(position).orderName);
        holder.tv_amount.setText("¥"+list.get(position).orderAmount);
        final int pos = holder.getLayoutPosition();

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(holder.itemView, pos);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{
        TextView tv_id;
        TextView tv_name;
        TextView tv_amount;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv_id = (TextView) itemView.findViewById(R.id.tv_id);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_amount = (TextView) itemView.findViewById(R.id.tv_amount);
        }
    }
}
